package pokemon;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class DataReader 
{
	private final static String noAbil="(NoAbility)";
	public Pokedex makeDex()
	{  
		Pokedex dex = new Pokedex();

		try
		{
			String nextLine;
			BufferedReader pokeReader = new BufferedReader(new FileReader("pokeList.txt"));
			try 
			{
				while((nextLine = pokeReader.readLine()) != null)
				{
					String result[] = nextLine.split(" ");
					String result2[] = result[0].split(":");
					if(Pokedex.abil.getAbility(result[4])!=null||Pokedex.abil.getAbility(result[5])!=null||Pokedex.abil.getAbility(result[6])!=null)
					{
						dex.add(new IndividualPokemon(Integer.parseInt(result2[0]), Integer.parseInt(result2[1]), result[1], 
								Types.getType(result[2]), Types.getType(result[3]),	result[4],
								Integer.parseInt(result[7]), Integer.parseInt(result[8]), Integer.parseInt(result[9]),
								Integer.parseInt(result[10]), Integer.parseInt(result[11]), Integer.parseInt(result[12])));
						if(!result[5].equals(noAbil))
						{
							dex.add(new IndividualPokemon(Integer.parseInt(result2[0]), Integer.parseInt(result2[1]), result[1], 
									Types.getType(result[2]), Types.getType(result[3]),	result[5],
									Integer.parseInt(result[7]), Integer.parseInt(result[8]), Integer.parseInt(result[9]),
									Integer.parseInt(result[10]), Integer.parseInt(result[11]), Integer.parseInt(result[12])));
						}
						if(!result[6].equals(noAbil))
						{
							dex.add(new IndividualPokemon(Integer.parseInt(result2[0]), Integer.parseInt(result2[1]), result[1], 
									Types.getType(result[2]), Types.getType(result[3]),	result[6],
									Integer.parseInt(result[7]), Integer.parseInt(result[8]), Integer.parseInt(result[9]),
									Integer.parseInt(result[10]), Integer.parseInt(result[11]), Integer.parseInt(result[12])));
						}
					}
					else
					{
						dex.add(new Pokemon(Integer.parseInt(result2[0]), Integer.parseInt(result2[1]), result[1], 
								Types.getType(result[2]), Types.getType(result[3]),	result[4], result[5], result[6],
								Integer.parseInt(result[7]), Integer.parseInt(result[8]), Integer.parseInt(result[9]),
								Integer.parseInt(result[10]), Integer.parseInt(result[11]), Integer.parseInt(result[12])));
					}
					
				}

			} 
			catch (IOException e) 
			{
				System.err.println("Error reading file.");
				System.exit(1);
			}
		} 
		catch (FileNotFoundException e) 
		{
			System.err.println("File not found.");
			System.exit(1);
		}
		return dex;
	}
	
	public static void reader()
	{
		ArrayList<Pokemon> pokemon = new ArrayList<Pokemon>();
		ArrayList<String> types = new ArrayList<String>();
		ArrayList<String> abilities = new ArrayList<String>();
		String nextLine;
		int count = 0;
		try 
		{			
			BufferedReader pokeReader = new BufferedReader(new FileReader("abilities.txt"));
			while((nextLine = pokeReader.readLine()) != null)
			{
				abilities.add(nextLine);
			}
			
			pokeReader = new BufferedReader(new FileReader("types.txt"));
			while((nextLine = pokeReader.readLine()) != null)
			{
				types.add(nextLine);
			}
			
			pokeReader=new BufferedReader(new FileReader("pokemons.txt"));
			while((nextLine=pokeReader.readLine())!=null)
			{
				String result[]=nextLine.split(" ");
				String result2[]=result[0].split(":");
				pokemon.add(new Pokemon(Integer.parseInt(result2[0]),Integer.parseInt(result2[1]),result[1],Types.getType("VOID"),Types.getType("VOID"),noAbil,noAbil,noAbil,0,0,0,0,0,0));
			}
			
			//Next part gets each pokes type, adds it to the correct one.
			count=0;
			pokeReader=new BufferedReader(new FileReader("poke_type1-5G.txt"));
			while((nextLine=pokeReader.readLine())!=null)
			{
				//System.out.println(nextLine);
				String result[]=nextLine.split(" ");
				String result2[]=result[0].split(":");
				for(Pokemon p:pokemon)
				{
					if(p.getId()==Integer.parseInt(result2[0])&&p.getForm()==Integer.parseInt(result2[1]))
					{
						count=pokemon.indexOf(p);
					}
				}
				//Rather then get at count, get the one with the same id and form number;
				pokemon.get(count).setType1(Types.getType(types.get(Integer.parseInt(result[1]))));
			}
			
			pokeReader=new BufferedReader(new FileReader("poke_type2-5G.txt"));
			count=0;
			while((nextLine=pokeReader.readLine())!=null)
			{
				String result[]=nextLine.split(" ");
				String result2[]=result[0].split(":");
				for(Pokemon p:pokemon)
				{
					if(p.getId()==Integer.parseInt(result2[0])&&p.getForm()==Integer.parseInt(result2[1]))
					{
						count=pokemon.indexOf(p);
					}
				}
				pokemon.get(count).setType2(Types.getType(types.get(Integer.parseInt(result[1]))));
			}
			
			//Abilities now...
			count=0;
			pokeReader=new BufferedReader(new FileReader("poke_ability1_5G.txt"));
			while((nextLine=pokeReader.readLine())!=null)
			{
				String result[]=nextLine.split(" ");
				String result2[]=result[0].split(":");
				for(Pokemon p:pokemon)
				{
					if(p.getId()==Integer.parseInt(result2[0])&&p.getForm()==Integer.parseInt(result2[1]))
					{
						count=pokemon.indexOf(p);
					}
				}
				pokemon.get(count).setAbility1(abilities.get(Integer.parseInt(result[1])));
			}
			
			pokeReader=new BufferedReader(new FileReader("poke_ability2_5G.txt"));
			count=0;
			while((nextLine=pokeReader.readLine())!=null)
			{
				String result[]=nextLine.split(" ");
				String result2[]=result[0].split(":");
				for(Pokemon p:pokemon)
				{
					if(p.getId()==Integer.parseInt(result2[0])&&p.getForm()==Integer.parseInt(result2[1]))
					{
						count=pokemon.indexOf(p);
					}
				}
				pokemon.get(count).setAbility2(abilities.get(Integer.parseInt(result[1])));
			}
			
			pokeReader=new BufferedReader(new FileReader("poke_ability3_5G.txt"));
			count=0;
			while((nextLine=pokeReader.readLine())!=null)
			{
				String result[]=nextLine.split(" ");
				String result2[]=result[0].split(":");
				for(Pokemon p:pokemon)
				{
					if(p.getId()==Integer.parseInt(result2[0])&&p.getForm()==Integer.parseInt(result2[1]))
					{
						count=pokemon.indexOf(p);
					}
				}
				pokemon.get(count).setAbility3(abilities.get(Integer.parseInt(result[1])));
			}
			
			pokeReader=new BufferedReader(new FileReader("poke_stats.txt"));
			count=0;
			while((nextLine=pokeReader.readLine())!=null)
			{
				String result[]=nextLine.split(" ");
				String result2[]=result[0].split(":");
				for(Pokemon p:pokemon)
				{
					if(p.getId()==Integer.parseInt(result2[0])&&p.getForm()==Integer.parseInt(result2[1]))
					{
						count=pokemon.indexOf(p);
					}
				}
				pokemon.get(count).setHp(Integer.parseInt(result[1]));
				pokemon.get(count).setAttack(Integer.parseInt(result[2]));
				pokemon.get(count).setDefense(Integer.parseInt(result[3]));
				pokemon.get(count).setSpecAtk(Integer.parseInt(result[4]));
				pokemon.get(count).setSpecDef(Integer.parseInt(result[5]));
				pokemon.get(count).setSpeed(Integer.parseInt(result[6]));
			}
		}
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		//System.out.println(pokemon.get(151).getName()+" "+pokemon.get(151).getType2().name());
//		System.out.println(pokemon.get(151).getName()+" "+pokemon.get(151).getType1().name()+" "+pokemon.get(151).getType2().name()
//				  +" "+pokemon.get(151).getAbility1()+" "+pokemon.get(151).getAbility2()+" "+pokemon.get(151).getAbility3()
//				  +" "+pokemon.get(151).getHp()+" "+pokemon.get(151).getAttack()+" "+pokemon.get(151).getDefense()
//				  +" "+pokemon.get(151).getSpecAtk()+" "+pokemon.get(151).getSpecDef()+" "+pokemon.get(151).getSpeed());
		for(int i=0;i<pokemon.size();i++)
		{
			System.out.println(pokemon.get(i).getId()+":"+pokemon.get(i).getForm()+" "+pokemon.get(i).toString()+" ");
		}
		
	}
}
